/*
 * Systemprogrammierung
 * Multiplayer-Quiz
 *
 * Gemeinsam verwendete Module
 * 
 * rfc.h: Definitionen für das Netzwerkprotokoll gemäß dem RFC
 */

#ifndef RFC_H
#define RFC_H

#include "common/question.h"

/*
 * RFC Typdefinition
 */

//#define RFC_ERRORUNKNOWN	 	 0//kein Paket,lesen fehlerhaft
#define RFC_LOGINREQUEST         1//
#define RFC_LOGINRESPONSEOK      2//
#define RFC_CATALOGREQUEST       3 //muss man nicht im Content definieren, da header
#define RFC_CATALOGRESPONSE      4//
#define RFC_CATALOGCHANGE        5//
#define RFC_PLAYERLIST           6//
#define RFC_STARTGAME            7//
#define RFC_QUESTIONREQUEST      8//
#define RFC_QUESTION             9//
#define RFC_QUESTIONANSWERED     10//
#define RFC_QUESTIONRESULT       11//
#define RFC_GAMEOVER             12//
#define RFC_ERRORWARNING         255// Error

//Packen der Struckts auf minimale Größe
#pragma pack(push,1)

typedef struct{
 	char playername[32];
 	uint32_t score;
 	uint8_t id;

  }PLAYERLIST;

 typedef struct {
	 uint8_t errortype;
	 char errormessage [100];

 }ERROR;

 typedef struct{
 	uint8_t TimedOut;
 	uint8_t correct;
 }QUESTIONRESULT;


 typedef union {

	    char playername[32];          		 //LoginRequest
	    uint8_t clientid;            		//LoginResponseok
	    								   //CatalogRequest,hier nichts notwendig
	    char catalogname[31];	   		  //CatalogResponse, CatalogChange,StartGame
	    PLAYERLIST playerlist[4]; 	     //Playerlist
	    QuestionMessage question;     	    //Question
	    uint8_t selection;       	   //QuestionAnswered
	    QUESTIONRESULT antwort;// QuestionResult
	    uint8_t playerrank;			 // GameOver
	    ERROR error;
  }CONTENT;

//Anfang der Protokolle
typedef struct{
	uint8_t type;
	uint16_t length;

}HEADER;
HEADER head;

//komplette Struktur der Protokolle
typedef struct{
	HEADER header;
	CONTENT content;
}PACKET;

#pragma pack(pop)

int openServerSocket(unsigned short);
void packetSchreiben(PACKET, int);
PACKET packetLesen (int);

#endif
